package DTO;


import DTO.MailStructureDTO;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

public  class TableMailDataDTO extends AbstractTableModel {
        private String[] _columnNames = {"Mask","Subject","Sender","Date Send","iSAttach"};
        private ArrayList<Object> _data;
        private ArrayList<MailStructureDTO> _realData;


        public TableMailDataDTO(){
            _data = new ArrayList<Object>();
            _realData = new ArrayList<MailStructureDTO>();

        }
        public void setTypeMail(int type, int rows){
            
            Object value = getValueAt(rows, 0);
            int index = _data.indexOf(value);
            value = setTypeMailIcon(type);
            setValueAt(value, rows, 0);
//            Object setvalue
//            fireTableCellUpdated(rows,0);
        }


        public ImageIcon setTypeMailIcon(int type){
            ImageIcon icon;
            switch(type){
                case 1://mask as read
                    icon = new ImageIcon("Image/Message.png");
                    break;
                case 2: //mask as unread
                    icon = new ImageIcon("Image/unread.png");
                    break;
                case 3: //mask as Important
                    icon = new ImageIcon("Image/important.png");
                    break;
                case 4 : //mask as personal
                    icon = new ImageIcon("Image/personal.jpg");
                    break;
                default:
                     icon = new ImageIcon("Image/flag-blue.png");
            }
            return icon;
        }
        public void addRow(MailStructureDTO mail){

            ImageIcon icon = new ImageIcon();
            ImageIcon icon2 = new ImageIcon();
            //if ( mail.getIsFlag() == true)
                icon2 = setTypeMailIcon(mail.getTypeMail());
            if ( mail.getIsAttachment() == true)
                icon = new ImageIcon("Image/Pin.png");
            Object value[]={icon2,mail.getTitle(),mail.getSender(),mail.getDateSend() ,icon};

            try{
                _data.add(value);
                _realData.add(mail);
                //getRealData().add(mail);
            }catch(Exception ex){
                JOptionPane.showMessageDialog(null, ex);
            }

            this.fireTableDataChanged();

        }
        public int getRowCount() {
            //throw new UnsupportedOperationException("Not supported yet.");
            return _data.size();
        }

        public int getColumnCount() {
            //throw new UnsupportedOperationException("Not supported yet.");
             return _columnNames.length;
        }
        @Override
         public String getColumnName(int col) {
            return _columnNames[col];
        }
         /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
         */
        @Override
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        /*
         * Don't need to implement this method unless your table's
         * editable.
         */
        @Override
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
//            if (col < 2) {
//                return false;
//            } else {
//                return true;
//            }
            return false;
        }

        /*
         * Don't need to implement this method unless your table's
         * data can change.
         */
        @Override
        public void setValueAt(Object value, int row, int col) {
            //_data.add(value);
            ((Object[])(_data.get(row)))[col] = value;
            fireTableCellUpdated(row, col);

        }
        public Object getValueAt(int rowIndex, int columnIndex) {
            //throw new UnsupportedOperationException("Not supported yet.");
             return ((Object[])(_data.get(rowIndex)))[columnIndex];
        }

        /**
         * @return the _realData
         */
        public ArrayList<MailStructureDTO> getRealData() {
            return _realData;
        }

        /**
         * @param realData the _realData to set
         */
        public void setRealData(ArrayList<MailStructureDTO> realData) {
            this._realData = realData;
        }



    }
